package com.arcvideo.acsplayer.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class HttpUtil {

	private static AsyncHttpClient client = new AsyncHttpClient(); // 实例话对象

	static {

		client.setTimeout(10000); // 设置链接超时，如果不设置，默认为10s
	}

	// 用一个完整url获取一个string对象
	public static void get(String urlString, AsyncHttpResponseHandler res) {

		client.get(urlString, res);

	}

	// url里面带参数
	public static void get(String urlString, RequestParams params, AsyncHttpResponseHandler res) {

		client.get(urlString, params, res);

	}

	// 不带参数，获取json对象或者数组
	public static void get(String urlString, JsonHttpResponseHandler res) {
		client.get(urlString, res);

	}

	// 带参数，获取json对象或者数组
	public static void get(String urlString, RequestParams params, JsonHttpResponseHandler res) {

		client.get(urlString, params, res);

	}

	// 下载数据使用，会返回byte数据
	public static void get(String uString, BinaryHttpResponseHandler bHandler) {

		client.get(uString, bHandler);

	}

	public static AsyncHttpClient getClient() {

		return client;
	}

	public static String getMD5Digest(String origin) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		byte[] messageDigest = MessageDigest.getInstance("MD5").digest(origin.getBytes("utf-8"));
		BigInteger number = new BigInteger(1, messageDigest);
		String md5 = number.toString(16);
		while (md5.length() < 32)
			md5 = "0" + md5;
		return md5;
	}

}